// component/room-monitoring/index.ts
import {
  getAllVillage,
  getAllBuilding,
  getAllUnit,
  getAlllList,
} from "../../api/room-monitoring";
type list = {
  text: String;
  value: Number | String;
};
Component({
  /**
   * 组件的属性列表
   */
  properties: {},

  /**
   * 组件的初始数据
   */
  data: {
    option1: [{ text: "小区", value: -1 }] as list[],
    option2: [{ text: "楼栋", value: -1 }] as list[],
    option3: [{ text: "单元", value: -1 }] as list[],
    option4: [{ text: '全部', value: -1 }] as list[],
    village: -1 as Number,
    villageName: '-1' as String,
    building: -1 as Number,
    buildingName: '-1' as String,
    unit: -1 as Number,
    unitName: '-1' as String,
    isBuilding: true as Boolean,
    isUnit: true as Boolean,
    comID: -1 as Number,
    list: [] as any,
    height: 1100,
    page:1,
    limit:10,
    count:0,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getAlllList() {
      const _this = this
      const comID = _this.data.comID;
      const village = _this.data.villageName;
      const building = _this.data.buildingName;
      const unit = _this.data.unitName;
      const page = _this.data.page;
      const limit = _this.data.limit
      getAlllList(
        comID,
        village == '-1' ? "" : village,
        building == '-1' ? "" : building,
        unit == '-1' ? "" : unit,
        page,
        limit
      ).then((res: any) => {
        _this.setData({
          count:res.data.count
        })
        // 转换后的二维数组
        const arrayTwo = Object.values(
          res.data.data.reduce((res, item) => {
            res[item.village]
              ? res[item.village].push(item)
              : (res[item.village] = [item]);
            return res;
          }, {})
        );
        // const arr = arrayTwo.slice(15,196)
        // console.log(arrayTwo);
        const arr = [..._this.data.list,...arrayTwo]
        _this.setData({
          list: arr
        })
        console.log(_this.data.list)
      });
    },
    getAllVillage() {
      this.setData({
        option1: [{ text: "小区", value: -1 }],
      });
      let comID = this.data.comID;
      getAllVillage(comID).then((res: any) => {
        let list: list[] = [];
        res.data.data.map((item: any) => {
          return list.push({
            text: item.villageName,
            value: item.villageID,
          });
        });
        this.setData({
          option1: [...this.data.option1, ...list],
        });
        this.getAlllList();
        // console.log(res)
      });
    },
    handleVillage(e: any) {
      // console.log(e);
      let villageID = e.detail.value;
      let villageName = e.detail.text;
      const comID = wx.getStorageSync("userInfo").data.comID
      getAllBuilding(comID,villageID, villageName).then((res: any) => {
        let list: list[] = [];
        if (res.data.code == 200) {
          res.data.data.map((item: any) => {
            return list.push({
              text: item.buildingName,
              value: item.buildingID,
            });
          });
          this.setData({
            option2: [...this.data.option2, ...list],
            isBuilding: false,
            villageName: villageName,
            building: -1,
            unit: -1
          });
          this.getAlllList();
        } else {
          this.setData({
            list: [],
            isBuilding: true,
            isUnit: true,
            building: -1,
            unit: -1
          })
        }
      });
    },
    handleBuilding(e: any) {
      let buildingID = e.detail.value;
      let buildingName = e.detail.text;
      const comID = wx.getStorageSync("userInfo").data.comID
      getAllUnit(comID,buildingID, buildingName).then((res: any) => {
        let list: list[] = [];
        res.data.data.map((item: any) => {
          return list.push({
            text: item.unitName,
            value: item.unitID,
          });
        });
        this.setData({
          option3: [...this.data.option3, ...list],
          isUnit: false,
          buildingName: buildingName
        });
        this.getAlllList();
        // console.log(res)
      });
    },
    handleUnit(e: any) {
      let unitName = e.detail.text;
      this.setData({
        unitName: unitName
      })
      this.getAlllList();
    },
    getAlll() {
      this.setData({
        villageName: '-1',
        buildingName: '-1',
        unitName: '-1',
        village: -1,
        building: -1,
        unit: -1,
        isBuilding: true,
        isUnit: true
      })
      this.getAlllList();
    },
    onScrollToLower(){
      const count = this.data.count;
      const list = this.data.list
      if(count!=list.length){
        let num = this.data.page + 1
        this.setData({
          page:num
        });
        this.getAlllList();
      }
    },
    onDetail(e:any){
      wx.navigateTo({
        url:'/subpackagesA/pages/room-monitoring-detail/room-monitoring-detail?roomId=' + e.currentTarget.dataset.roomid
      })
    }
  },
  //小程序声明周期
  lifetimes: {
    attached: function () {
      // 在组件实例进入页面节点树时执行

    },
    detached: function () {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  pageLifetimes: {
    show: function () {
      // 页面被展示
      this.setData({
        comID: wx.getStorageSync("userInfo").data.comID,
        //   userID:wx.getStorageSync("userInfo").data.userID
      });
      this.getAllVillage();
    },
    hide: function () {
      // 页面被隐藏
    },
    resize: function (size) {
      // 页面尺寸变化
    },
  },
});
